<template>
  <div class="strategy-list">
    <el-table :data="tableData" stripe style="width: 100%" max-height="600">
      <el-table-column prop="createTime" label="批注人">
        <template slot-scope="scope">
          {{ scope.row.sysUser | showUserInfo }}
        </template>
      </el-table-column>
      <el-table-column prop="size" label="状态">
        <template slot-scope="scope">
          <span v-show="scope.row.auditStatus == 1"> 建议 </span>
          <span class="color-success" v-show="scope.row.auditStatus == 2">
            采纳
          </span>
          <span class="color-waring" v-show="scope.row.auditStatus == 3">
            有用
          </span>
        </template>
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="320">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button type="success" size="mini" @click="handleDetail(row)">
              预览
            </el-button>
            <el-button type="primary" size="mini" @click="handleUse(row, 2)">
              采纳
            </el-button>
            <el-button type="primary" size="mini" @click="handleUse(row, 3)">
              有用
            </el-button>
            <el-button type="primary" size="mini" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
    <Edit @e-refreList="getList" @e-refreListAndUse="handleUse" />
    <ViewDetail />
  </div>
</template>
<script>
import {
  sysKnowledgeStrategyQuery,
  sysKnowledgeStrategyDelete,
  sysKnowledgeStrategyAdopt,
} from '@/api/admin/courseLibrary/sysKnowledgeStrategy.js'
import Bus from '@/utils/bus.js'
import { setKnoledgeInfo } from '../../mixins/refreshKnowledgeById'
export default {
  mixins: [setKnoledgeInfo],
  components: {
    Edit: () => import('./Edit.vue'),
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        resId: '',
        resType: 1,
      },
      tableData: [],
      pageObj: {},
    }
  },
  mounted() {
    this.getList()
    Bus.$on('refreshStrategyList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
    Bus.$on('sendKnowledgeId', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    Bus.$off('refreshStrategyList')
    Bus.$off('sendKnowledgeId')
  },
  methods: {
    getList() {
      if (!this.listQuery.resId) return
      sysKnowledgeStrategyQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.pageObj = res.data
      })
    },
    //预览
    handleDetail(row) {
      Bus.$emit('showDialogViewDetail', row, '批注详情')
    },
    handleUse(row, auditStatus = 2) {
      // status： 1建议  2采纳 3有用
      sysKnowledgeStrategyAdopt({ id: row.id, auditStatus }).then((res) => {
        this.getList()
        // this.getInfoById(); //更新store中数据
      })
    },
    //编辑
    handleEdit(row) {
      Bus.$emit('showDialogStrategy', row)
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      sysKnowledgeStrategyDelete(row.id).then((res) => {
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
  },
}
</script>
<style scoped>
.strategy-content {
  max-width: 500px;
}
</style>